
/*******************************************************************************
*																			   *
*  	PURPOSE:  			Replicate tables and figures				 		   *
*																			   *
*  	Last update:  		October 2024										   *
*																			   *
********************************************************************************
						
	** REQUIRES:   		"${DATADIR}/data_all_final.dta"	
							
	** SECTIONS:   		0. Settings
	   (in bookmarks)		0.a	Standardize settings and programs
							0.b	Set folder path globals
							0.c	Data settings and macros
						1. Main tables and figures 
							1.a Figure 1
							1.b Table 1
							1.c	Table 2
							1.d	Table 3
							1.e	Table 4
							1.f	Table 5
							1.g	Table 6
						2. Tables and Figures - Appendix
							2.a	Figure A.1
							2.b Figure A.4
							2.c Figure A.5
							2.d	Table A.1
							2.e	Table A.2
							2.f	Table A.3
							2.g	Table A.4
							2.h	Table A.5
							2.i	Table A.6
							2.j	Table A.7
							2.k	Table A.8
							2.l	Table A.9
							2.m	Table A.10
							2.n	Table A.15
							2.o	Table A.16
							2.p	Table A.17

*******************************************************************************/
**# 	0. Settings				 	
*******************************************************************************/
**## 	0.a Standardize settings and programs
/*----------------------------------------------------------------------------*/
{
	
	* Preamble
	clear 	all 
	set		maxvar 30000
	set 	matsize 10000
	set 	more off
	set 	varabbrev on
	set 	scheme s2color
	
	* Program 1 to set p-values (useful all throughout the file)
	capture program drop set_pval_type1
		
	program define set_pval_diff_incentives
	
	test	2.incentives = 1.incentives
	global	pval1 = r(p) 
	test	3.incentives = 1.incentives
	global	pval2 = r(p) 
	test	2.incentives = 3.incentives
	global	pval3 = r(p) 

	end

}

	
/*----------------------------------------------------------------------------*/
**## 	0.b Set folder path globals
/*----------------------------------------------------------------------------*/
{
					
	* Working directory (root folder) 
	* -----------------
	
	if 		c(username) == 	" " {
			/* you can find your username by running "di c(username)" in the 
			command window, and then subsitute it here */
		
	global 	LOCALDIR " "	//add the path where you saved the folder
	}
		
	* Subfolders
	* ----------
	
	global 		  DATAWORK 	" "
				/* to fill with directory path*/

	
	global 		   DATADIR 	"${DATAWORK}/Data"
	global 		 TABLESDIR 	"${DATAWORK}/Results/Tables"
	global 		FIGURESDIR 	"${DATAWORK}/Results/Figures"
	
	cd 		"${TABLESDIR}"
	
}
	
	
/*----------------------------------------------------------------------------*/
**## 	0.c Data settings, macros, and tempfiles
/*----------------------------------------------------------------------------*/	
{
	
	* Load the dataset
	use 		"${DATADIR}/data_all_final.dta", clear
	
	* Macros for regressions  
	global 	      controls 	i.district i.distance_abvmedian	i.cluster_size
							
	global		 controls1	district distance_abvmedian	cluster_size
																					
	* Set table general formatting options for [outreg2]
	global	 outreg0ptions  nor2 adec(3) bdec(3) nocons excel label
		
	* Seed (paper standard: fig = 111 | tab = 123)
	global	   	  seed_fig	"111"
	global	   	  seed_tab	"123"
	
	* Bootstrap repetitions (paper standard: fig = 100 | tab = 250)
	global	   bs_reps_fig	"100"
	global	   bs_reps_tab	"250"
	
	* Standardize settings across users
	ssc 		   install 	outreg2, replace
	ssc 		   install 	coefplot, replace
	ssc 		   install	rwolf2, replace
	
}


********************************************************************************
**#  	1. Main tables and figures 				  							  
********************************************************************************
**## 	1.a Figure 1:	Effect of Incentives on the Number of Visits
/*----------------------------------------------------------------------------*/
{
		
	local 	filename = "Visits"
	capture erase "${FIGURESDIR}/`filename'.png"
	
	preserve
	
	recode	incentives (1 = 2) (2 = 1), pre(rec_)

	sum		HH_nb_visits_E if rec_incentives == 0, meanonly
	local 	mc: di %12.3fc r(mean)
	local 	mc = trim("`mc'")
	
	#d	;
		reg HH_nb_visits_E
			i.rec_incentives
			$controls
		,
		cl(SUPV_id)
		;
	#d	cr
	
	gen 	b_nb_visits = _b[1.rec_incentives] if rec_incentives == 1
	replace b_nb_visits = _b[2.rec_incentives] if rec_incentives == 2
	replace b_nb_visits = _b[3.rec_incentives] if rec_incentives == 3
	
	gen 	se_nb_visits = _se[1.rec_incentives] if rec_incentives == 1
	replace se_nb_visits = _se[2.rec_incentives] if rec_incentives == 2
	replace se_nb_visits = _se[3.rec_incentives] if rec_incentives == 3

	collapse b_nb_visits se_nb_visits (count) n = HH_nb_visits_E				///
			, by(rec_incentives)
	gen 	h_nb_visits = b_nb_visits + invttail(n-1,0.025) * se_nb_visits
	gen 	l_nb_visits = b_nb_visits - invttail(n-1,0.025) * se_nb_visits
	
	drop if rec_incentives == 0
	
	#d	;
		twoway 	(bar b_nb_visits rec_incentives , lcolor(black) fcolor(erose))
				(rcap h_nb_visits l_nb_visits rec_incentives)
		,
		xlabel(	1 `" "Worker incentives" "vs. control" "' 
				2 `" "Supervisor incentives" "vs. control" "' 
				3 `" "Shared incentives" "vs. control" "')
		ytitle(Number of visits) 
		xtitle("") 
		legend(off) 
		graphregion(color(white)) 
		ysc(r(0)) 
		ylabel(1(1)4)
		note("Mean in control = `mc'", pos(11))
		;
	#d	cr
	
	graph export "${FIGURESDIR}/`filename'.png", replace
	
	restore
	
}	


/*----------------------------------------------------------------------------*/
**## 	1.b Table 1:	Summary Statistics and Balance Checks
/*----------------------------------------------------------------------------*/
{
	
	local 	filename = "Sum Stats"
	capture erase "${TABLESDIR}/`filename'.txt"
	capture erase "${TABLESDIR}/`filename'.xls"
	
	local		  SPcharac	SP_male_B SP_age_B SP_primary_educ_B 				///
							SP_secondary_above_educ_B SP_wealth_B SP_nb_WKs_B

	local 		  WKcharac 	WK_male_B WK_age_B WK_primary_educ_B 				///
							WK_secondary_above_educ_B WK_wealth_B WK_nb_HHs_B 	///
							WK_dist_SUPV_B

	local 		  HHcharac 	HH_age_B HH_primary_educ_E 							///
							HH_secondary_above_educ_E HH_wealth_B 				///
							HH_dist_village_E
						
	local 	 Villagecharac	WK_road_phu_commu_E WK_mobile_net_commu_E
	
	local	  Healthcharac	SP_preagnanttreated_B SP_birthfacility_B SP_FIC_B 	///
							SP_malariatreated_B SP_diarrheatreated_B
																					
	foreach	x in SPcharac WKcharac HHcharac Villagecharac Healthcharac {
		
	preserve
	
	if		"`x'" == "SPcharac" | "`x'" == "Healthcharac"   {	
		
	collapse `SPcharac' `Healthcharac' incentives $controls1, by(SUPV_id)
	
	}

	foreach	y of varlist ``x'' {
				
	#d	;
		reg	`y'
			i.incentives
			$controls
		,
		cl(SUPV_id)
		;
	#d	cr
	
	if		"`x'" == "PScharac" {	
		
	#d	;
		reg	`y'
			i.incentives
			$controls
		,
		robust
		;
	#d	cr
	
	}

	sum		`y'
	local	c1`y': di %12.3fc r(mean)
	local	c2`y' = r(sd) 
	sum		`y' if incentives == 0
	local	c3`y' = r(mean)
	local	c4`y' = r(sd)
	sum		`y' if incentives == 2
	local	c5`y' = r(mean)
	local	c6`y' = r(sd)
	sum		`y' if incentives == 1
	local	c7`y' = r(mean)
	local	c8`y' = r(sd)
	sum		`y' if incentives == 3
	local	c9`y' = r(mean)
	local	c10`y' = r(sd)
	test	2.incentives 1.incentives 3.incentives
	local	c11`y' = r(p) 
	
	#d	;
		outreg2 
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}
		drop(i.incentives $controls)
		addstat(	Mean All, `c1`y'', 
					S.D. All, `c2`y'', 
					Mean Control, `c3`y'', 
					S.D. Control, `c4`y'', 
					Mean Worker Incentives Treatment, `c5`y'', 
					S.D. Worker Incentives Treatment, `c6`y'', 
					Mean Supv Incentives Treatment, `c7`y'', 
					S.D. Supv Incentives Treatment, `c8`y'', 
					Mean Shared Incentives Treatment, `c9`y'', 
					S.D. Shared Incentives Treatment, `c10`y'', 
					F-test of joint significance (p-value), `c11`y'')
		;
	#d	cr
	
	}
	
	restore
	
	}
	
}


/*----------------------------------------------------------------------------*/
**## 	1.c Table 2:	Visits
/*----------------------------------------------------------------------------*/
{
	
	local 	filename = "Visits"
	capture erase "${TABLESDIR}/`filename'.txt"
	capture erase "${TABLESDIR}/`filename'.xls"

	local	outcomes	HH_nb_visits_E HH_av_visit_length_E 					///
						HH_av_visit_lengthCOND_E HH_nb_topics_discussed_E 		///
						HH_HH_nb_topicsCOND_E HH_perc_visited_E 				///
						HH_visit_types_E HH_trust_E

	foreach y in `outcomes' {
		
	#d	;
		reg	`y'
			i.incentives
			$controls
		,
		cl(SUPV_id)
		;
	#d	cr

	sum		`y', meanonly
	local	mean_dep = r(mean)

	sum		`y' if incentives == 0, meanonly
	local	mean_dep_control = r(mean)

	* Program to set p-values
	set_pval_diff_incentives 

	#d	;
		outreg2
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}
		drop($controls)
		sortvar(2.incentives
				1.incentives
				3.incentives)
		addstat(Mean dep. var., `mean_dep', 
				Mean dep. var. in Control, `mean_dep_control', 
				P-value Worker = Supv, $pval1, 
				P-value Supv = Shared, $pval2, 
				P-value Worker = Shared, $pval3) 
		;
	#d	cr
	
	}
	
}


/*----------------------------------------------------------------------------*/
**## 	1.d Table 3:	Health Outcomes
/*----------------------------------------------------------------------------*/ 
{
	
	local 	filename = "Health"
	capture erase "${TABLESDIR}/`filename'.txt"
	capture erase "${TABLESDIR}/`filename'.xls"

	local	outcomes	HH_zscore_natal_E HH_ANC_atleast4_E 					///
						HH_instit_assisted_birth_E HH_PNC_PHUwithin_2days_E 	///
						HH_sixmonths_breastfeading_E 							///
						HH_uptodate_child_vaccine_card_E HH_zscore_disease_E 	///
						HH_fever_incidence_below5_E HH_cough_incidence_below5_E ///
						HH_diarrhoea_incidence_below5_E

	foreach y in `outcomes' {

	#d	;
		reg	`y' 
			i.incentives
			$controls
		,
		cl(SUPV_id)
		;
	#d	cr

	sum		`y', meanonly
	local	mean_dep = r(mean)

	sum		`y' if incentives == 0, meanonly
	local	mean_dep_control = r(mean)

	* Program to set p-values
	set_pval_diff_incentives

	#d	;
		outreg2 
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}
		drop($controls)
		sortvar(2.incentives
				1.incentives
				3.incentives)
		addstat(Mean dep. var., `mean_dep', 
				Mean dep. var. in Control, `mean_dep_control', 
				P-value Worker = Supv, $pval1, 
				P-value Supv = Shared, $pval2, 
				P-value Worker = Shared, $pval3) 
		;
	#d	cr
	
	}
	
}


/*----------------------------------------------------------------------------*/
**## 	1.e Table 4:	Reporting and Incentives Payouts
/*----------------------------------------------------------------------------*/
{
	
	local 	filename = "Reporting"
	capture erase "${TABLESDIR}/`filename'.txt"
	capture erase "${TABLESDIR}/`filename'.xls"

	local	outcomes	WK_reports_permonth_E WK_reporting_rate_E 				///
						WK_WKearn_reports_E WK_SUPVearn_reports_E 				///
						WK_PHUearn_reports_E

	foreach	y in `outcomes' {

	#d	;
		reg	`y' 
			i.incentives
			$controls
		,
		cl(SUPV_id)
		;
	#d	cr

	sum		`y', meanonly
	local	mean_dep = r(mean)

	sum		`y' if incentives == 0, meanonly
	local	mean_dep_control = r(mean)

	* Program to set p-values
	set_pval_diff_incentives

	#d	;
		outreg2 
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}
		drop($controls)
		sortvar(2.incentives
				1.incentives
				3.incentives)
		addstat(Mean dep. var., `mean_dep', 
				Mean dep. var. in Control, `mean_dep_control', 
				P-value Worker = Supv, $pval1, 
				P-value Supv = Shared, $pval2, 
				P-value Worker = Shared, $pval3) 
		;
	#d	cr
	
	}
	
}


/*----------------------------------------------------------------------------*/
**## 	1.f Table 5:	Supervisor Effort
/*----------------------------------------------------------------------------*/
{
	
	local 	filename = "PSeffort"
	capture erase "${TABLESDIR}/`filename'.txt"
	capture erase "${TABLESDIR}/`filename'.xls"
	
	local	outcomes	HH_accomp_visits_E WK_meeting_last_month_E 				///
						WK_total_healthtest

	foreach	y in `outcomes' {

	#d	;
		reg	`y' 
			i.incentives
			$controls
		,
		cl(SUPV_id)
		;
	#d	cr

	sum		`y', meanonly
	local	mean_dep = r(mean)

	sum		`y' if incentives == 0, meanonly
	local	mean_dep_control = r(mean)

	* Program to set p-values
	set_pval_diff_incentives

	#d	;
		outreg2
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}				
		drop($controls)
		sortvar(2.incentives
				1.incentives
				3.incentives)
		addstat(Mean dep. var., `mean_dep', 
				Mean dep. var. in Control, `mean_dep_control', 
				P-value Worker = Supv, $pval1, 
				P-value Supv = Shared, $pval2, 
				P-value Worker = Shared, $pval3) 
		;
	#d	cr
	
	}
	
}


/*----------------------------------------------------------------------------*/
**## 	1.g Table 6:	Transfers
/*----------------------------------------------------------------------------*/
{
	
	local 	filename = "Transfers"
	capture erase "${TABLESDIR}/`filename'.txt"
	capture erase "${TABLESDIR}/`filename'.xls"
	
	local	outcomes	SP_shared_inc_E WK_shared_inc_E 						///
						WK_transfer_fromSUPV_E WK_transfer_toSUPV_E 			///
						WK_nettransfer_E

	foreach	y in `outcomes' {

	#d	;
		reg	`y'
			i.incentives
			$controls
		,
		cl(SUPV_id)
		;
	#d	cr
	
	sum		`y', meanonly
	local	mean_dep = r(mean)

	sum		`y' if incentives == 0, meanonly
	local	mean_dep_control = r(mean)

	* Program to set p-values
	set_pval_diff_incentives
	
	#d	;
		outreg2
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}
		drop($controls)
		sortvar(2.incentives
				1.incentives
				3.incentives)
		addstat(Mean dep. var., `mean_dep', 
				Mean dep. var. in Control, `mean_dep_control', 
				P-value Worker = Supv, $pval1, 
				P-value Supv = Shared, $pval2, 
				P-value Worker = Shared, $pval3) 
		;
	#d	cr
	
	}
	
}

	
********************************************************************************
**# 	2. Tables and Figures - Appendix			 							
********************************************************************************
**## 	2.a Figure A.1:	Monthly Evolution of SMS Reporting
/*----------------------------------------------------------------------------*/
{
	
	local 	filename = "graph_coef_reports_overtime"
	capture erase "${FIGURESDIR}/`filename'.png"
	
	local	months	reports_month1 reports_month2 reports_month3 reports_month4 ///
					reports_month5 reports_month6 reports_month7 reports_month8 ///
					reports_month9 reports_month10

	foreach y in `months' {
				
	#d	;
		eststo: 
		reg	WK_`y'_E
			i.incentives
			$controls
		,
		cl(SUPV_id)
		;
	#d	cr 
	
	}
	
	#d	;
		coefplot	(est1, 	keep(2.incentives) rename(2.incentives = Month1) \
					est2, 	keep(2.incentives) rename(2.incentives = Month2) \  
					est3, 	keep(2.incentives) rename(2.incentives = Month3) \  
					est4, 	keep(2.incentives) rename(2.incentives = Month4) \  
					est5, 	keep(2.incentives) rename(2.incentives = Month5) \  
					est6, 	keep(2.incentives) rename(2.incentives = Month6) \  
					est7, 	keep(2.incentives) rename(2.incentives = Month7) \  
					est8, 	keep(2.incentives) rename(2.incentives = Month8) \  
					est9, 	keep(2.incentives) rename(2.incentives = Month9) \ 
					est10, 	keep(2.incentives) rename(2.incentives = Month10) 
							label(Worker incentives vs. control))
							
					(est1, 	keep(1.incentives) rename(1.incentives = Month1) \
					est2, 	keep(1.incentives) rename(1.incentives = Month2) \ 
					est3, 	keep(1.incentives) rename(1.incentives = Month3) \  
					est4, 	keep(1.incentives) rename(1.incentives = Month4) \  
					est5, 	keep(1.incentives) rename(1.incentives = Month5) \  
					est6, 	keep(1.incentives) rename(1.incentives = Month6) \  
					est7, 	keep(1.incentives) rename(1.incentives = Month7) \ 
					est8, 	keep(1.incentives) rename(1.incentives = Month8) \ 
					est9, 	keep(1.incentives) rename(1.incentives = Month9) \ 
					est10, 	keep(1.incentives) rename(1.incentives = Month10)
							label(Supervisor incentives vs. control))
									
					(est1,	keep(3.incentives) rename(3.incentives = Month1) \ 
					est2, 	keep(3.incentives) rename(3.incentives = Month2) \ 
					est3, 	keep(3.incentives) rename(3.incentives = Month3) \  
					est4, 	keep(3.incentives) rename(3.incentives = Month4) \  
					est5, 	keep(3.incentives) rename(3.incentives = Month5) \  
					est6, 	keep(3.incentives) rename(3.incentives = Month6) \  
					est7, 	keep(3.incentives) rename(3.incentives = Month7) \  
					est8, 	keep(3.incentives) rename(3.incentives = Month8) \  
					est9, 	keep(3.incentives) rename(3.incentives = Month9) \  
					est10, 	keep(3.incentives) rename(3.incentives = Month10) 
							label(Shared incentives vs. control))
		, 
		ytitle(Number of reports) 
		xtitle(Month of the intervention) 
		vert
		coeflabels(	Month1 = "1"
					Month2 = "2"
					Month3 = "3" 
					Month4 = "4" 
					Month5 = "5" 
					Month6 = "6" 
					Month7 = "7"
					Month8 = "8"
					Month9 = "9"
					Month10 = "10")
		graphregion(color(white))
		;
	#d	cr
			
	graph export "${FIGURESDIR}/`filename'.png", replace
	
}


/*----------------------------------------------------------------------------*/
**## 	2.b Figure A.4:	Mediation Analysis
/*----------------------------------------------------------------------------*/
{
	
	local 	filename = "Mediation_PS_anyserv_vill_home"
	capture erase "${FIGURESDIR}/`filename'.png"

	* % Accompanied household visits
	* With Quintiles and controls
	preserve
	
	keep if incentives != 1 & incentives !=3

	local 	r1 = 1
	local 	r2 = 2/3
	local 	r3 = 0.5
	local 	r4 = 1/3
	local 	r5 = 0
	matrix 	def quint_PSE_m = [`r1',`r2', `r3', `r4',`r5']

	foreach val of numlist 1/5 {

	* Estimate demediation function
	gen		HH_accomp_visits_E_`val'											///
			= HH_accomp_visits_E - quint_PSE_m[1,`val']

	#d	;
		reg	HH_nb_visits_E
			2.incentives
			HH_accomp_visits_E_`val'
			c.HH_accomp_visits_E_`val'#2.incentives
			$controls 
		,
		cl(SUPV_id) 
		;
	#d	cr
	
	matrix 	A = e(b)

	* De-mediate the outcome
	gen 	y_hat_dem`val' = HH_nb_visits_E - 									///
							 (A[1,11]  + A[1,2] * HH_accomp_visits_E_`val'	 	///
							 + A[1,3]  * HH_accomp_visits_E_`val'				///
									   * (incentives==2)						///
							 + A[1,4]  * 1.district 							///
							 + A[1,5]  * 2.district  							///
							 + A[1,6]  * 3.district  							///
							 + A[1,7]  * 4.district  							///
							 + A[1,8]  * 5.district  							///
							 + A[1,9]  * 1.distance_abvmedian 					///
							 + A[1,10] * 1.cluster_size)

	* Second stage (for the average Supervisor effort)
	#d	;
		reg	y_hat_dem`val'
			2.incentives
			$controls
		,
		cl(SUPV_id)
		;
	#d	cr
	
	matrix	def	Result`val' = [_b[2.incentives] \ _se[2.incentives]]

	}

	matrix	def	AllRes = [Result1 \ Result2 \ Result3 \ Result4 \ Result5]
	drop 	y_hat_dem*
	matrix 	list AllRes

	restore

	* Figure
	local 	k = 0
	
	forval 	x = 1/5 {
		
	local 	++k
	local 	l = 2 * `k' - 1
	local 	j = 2 * `k'
	local 	b`k':	di AllRes[`l',1]
	local 	lb1`k':	di AllRes[`l',1] - invttail(e(df_r),.05)  * AllRes[`j',1]
	local 	lb2`k': di AllRes[`l',1] - invttail(e(df_r),.025) * AllRes[`j',1]
	local 	lb3`k': di AllRes[`l',1] - invttail(e(df_r),.005) * AllRes[`j',1]
	local 	ub1`k': di AllRes[`l',1] + invttail(e(df_r),.05)  * AllRes[`j',1]
	local 	ub2`k': di AllRes[`l',1] + invttail(e(df_r),.025) * AllRes[`j',1]
	local 	ub3`k': di AllRes[`l',1] + invttail(e(df_r),.005) * AllRes[`j',1]
	
	}

	matrix 	C = J(7,`k',.)
	matrix 	rownames C = coeff ll10 ul10 ll5 ul5 l11 ul1
	matrix 	colnames C = 1 2 3 4 5
	local	i = 0
	
	forval 	v = 1/`k' {
		
	local 	++i
	matrix 	C[1,`i'] = `b`i'' \ `lb1`i'' \ `ub1`i'' \ `lb2`i'' \ `ub2`i'' 		///
			\ `lb3`i'' \ `ub3`i''
	
	}
	
	matrix list C

	#d	;
		coefplot(matrix(C), color(erose) blcolor(black))
		,
		ci((6 7) (4 5) (2 3))
		ylabel(-1(1)4)
		xlabel(1 "0" 2 "1/3" 3 "1/2" 4 "2/3" 5 "1")
		recast(connected) 
		ciopts(	lwidth(3 ..) 
				lcolor(maroon*.1 maroon*.4 maroon*.7))
		legend(	order(1 "99% CI" 2 "95% CI" 3 "90% CI") 
				rows(1) 
				size(small))
		graphregion(color(white))	
		vertical
		xtitle("% Accompanied household visits", size(small))
		ytitle("Controlled Direct Effect (CDE)") 
		title("") 
		;
	#d	cr

	graph export "${FIGURESDIR}/`filename'.png", replace
	
}


/*----------------------------------------------------------------------------*/
**## 	2.c Figure A.5:	Visits and Supervisor Effort by Wealth and Distance
/*----------------------------------------------------------------------------*/
{
	
	preserve
	
	recode	incentives (1 = 2) (2 = 1), pre(rec_)

	keep 	rec_incentives $controls1 WK_rescaled_wealth_B 						///
			SP_rescaled_wealth_B HH_nb_visits_E HH_accomp_visits_E 				///
			HH_dist_village_E WK_dist_SUPV_B

	* Use demeaned outcome using the vs. controls
	local 	k = 0
	
	foreach y in HH_nb_visits_E HH_accomp_visits_E {
		
	local 	++k
	
	#d	;
		reg	`y' 
			$controls
		;
	#d	cr
	
	predict res_y`k' if `y'!=., res
	
	}

	* Number of visits - A1
	npregress kernel res_y1 WK_rescaled_wealth_B i.rec_incentives				///
			, vce(bootstrap, reps(${bs_reps_fig}) seed(${seed_fig})) 

	margins	r.rec_incentives, at(WK_rescaled_wealth_B=(0.125(0.125)0.75)) 		///
			vce(bootstrap, reps(${bs_reps_fig}) seed(${seed_fig}))

	#d	;
		marginsplot
		,
		recast(line)
		recastci(rarea)
		ciopts(color(%40))
		legend(	order(	1 "Worker incentives vs. control" 
						2 "Supervisor incentives vs. control" 
						3 "Shared incentives vs. control") 
				cols(1))
		ytitle(Number of visits) 
		graphregion(color(white)) 
		xtitle(Health worker's wealth) 
		title("")
		;
	#d	cr

	graph 	export "${FIGURESDIR}/HT_visits-CHWwealth-nonparametric.png"		///
			, replace
			
	* Number of visits - A2
	npregress kernel res_y1 SP_rescaled_wealth_B i.rec_incentives				///
			, vce(bootstrap, reps(${bs_reps_fig}) seed(${seed_fig}))

	margins r.rec_incentives, at(SP_rescaled_wealth_B=(0.125(0.125)0.75)) 		///
			vce(bootstrap, reps(${bs_reps_fig}) seed(${seed_fig}))

	#d	;
		marginsplot
		,
		recast(line)
		recastci(rarea)
		ciopts(color(%40))
		legend(	order(	1 "Worker incentives vs. control" 
						2 "Supervisor incentives vs. control" 
						3 "Shared incentives vs. control") 
				cols(1))
		ytitle(Number of visits) 
		graphregion(color(white)) 
		xtitle(Supervisor's wealth) 
		title("")
		;
	#d	cr

	graph	export "${FIGURESDIR}/HT_visits-PSwealth-nonparametric.png"			///
			, replace

	* Supervisor effort - A3
	npregress kernel res_y2 SP_rescaled_wealth_B i.rec_incentives				///
			, vce(bootstrap, reps(${bs_reps_fig}) seed(${seed_fig}))

	margins	r.rec_incentives, at(SP_rescaled_wealth_B=(0.125(0.125)0.75)) 		///
			vce(bootstrap, reps(${bs_reps_fig}) seed(${seed_fig}))

	#d	;
		marginsplot
		,
		recast(line)
		recastci(rarea)
		ciopts(color(%40))
		legend(	order(	1 "Worker incentives vs. control" 
						2 "Supervisor incentives vs. control" 
						3 "Shared incentives vs. control") 
				cols(1))
		ytitle("Supervisor effort index (PCA)") 
		graphregion(color(white)) 
		xtitle(Supervisor's wealth) 
		title("")
		;
	#d	cr

	graph	export "${FIGURESDIR}/HT_PSeffort-PSwealth-nonparametric.png"		///
			, replace

	** Figures by Distance
	* Number of visits - B1
	npregress kernel res_y1 HH_dist_village_E i.rec_incentives					///
			, vce(bootstrap, reps(${bs_reps_fig}) seed(${seed_fig}))

	margins	r.rec_incentives, at(HH_dist_village_E=(0(1)8)) 					///
			vce(bootstrap, reps(${bs_reps_fig}) seed(${seed_fig}))
	#d	;
		marginsplot
		,
		recast(line)
		recastci(rarea)
		ciopts(color(%40))
		legend(	order(	1 "Worker incentives vs. control" 
						2 "Supervisor incentives vs. control" 
						3 "Shared incentives vs. control") 
				cols(1))
		ytitle(	"% Household visits in which health" 
				"worker was accompanied by supervisor") 
		graphregion(color(white)) 
		xtitle(Average distance between health worker and households) 
		title("")
		;
	#d	cr

	graph 	export "${FIGURESDIR}/HT_visits-avgdistance-nonparametric.png"		///
			, replace
			
	* Number of visits - B2
	npregress kernel res_y1 WK_dist_SUPV_B i.rec_incentives						///
			, vce(bootstrap, reps(${bs_reps_fig}) seed(${seed_fig}))

	margins r.rec_incentives	, at(WK_dist_SUPV_B = (0(1)8)) 					///
			vce(bootstrap, reps(${bs_reps_fig}) seed(${seed_fig}))

	#d	;
		marginsplot
		,
		recast(line)
		recastci(rarea)
		ciopts(color(%40)) 
		legend(	order(	1 "Worker incentives vs. control" 
						2 "Supervisor incentives vs. control" 
						3 "Shared incentives vs. control") 
				cols(1))
		ytitle(Number of visits) 
		graphregion(color(white)) 
		xtitle(Distance between supervisor and health worker)
		title("")
		;
	#d	cr

	graph 	export "${FIGURESDIR}/HT_visits-distance-nonparametric.png"			///
			, replace

	* Supervisor effort - B3
	npregress kernel res_y2 WK_dist_SUPV_B i.rec_incentives 					///
			, vce(bootstrap, reps(${bs_reps_fig}) seed(${seed_fig}))

	margins r.rec_incentives, at(WK_dist_SUPV_B = (0(1)8)) 						///
			vce(bootstrap, reps(${bs_reps_fig}) seed(${seed_fig}))

	#d	;
		marginsplot
		,
		recast(line)
		recastci(rarea)
		ciopts(color(%40))
		legend(	order(	1 "Worker incentives vs. control" 
						2 "Supervisor incentives vs. control" 
						3 "Shared incentives vs. control") 
				cols(1))
		ytitle(	"% Household visits in which health" 
				"worker was accompanied by supervisor") 
		graphregion(color(white)) 
		xtitle(Distance between supervisor and health worker) 
		title("")
		;
	#d	cr

	graph 	export "${FIGURESDIR}/HT_percHHvisit-distance-nonparametric.png"	///
			, replace
			
	restore

}

			
/*----------------------------------------------------------------------------*/
**## 	2.d Table A.1:	APP Sum Stats P-values
/*----------------------------------------------------------------------------*/
{

	local 	filename = "Sum Stats P-values"
	capture erase "${TABLESDIR}/`filename'.txt"
	capture erase "${TABLESDIR}/`filename'.xls"
	
	local	      SPcharac 	SP_male_B SP_age_B SP_primary_educ_B 				///
							SP_secondary_above_educ_B SP_wealth_B SP_nb_WKs_B
		
	local 		  WKcharac	WK_male_B WK_age_B WK_primary_educ_B 				///
							WK_secondary_above_educ_B WK_wealth_B 				///
							WK_nb_HHs_B WK_dist_SUPV_B
						
	local  		  HHcharac	HH_age_B HH_primary_educ_E 							///
							HH_secondary_above_educ_E HH_wealth_B 				///
							HH_dist_village_E

	local 	 Villagecharac	WK_road_phu_commu_E WK_mobile_net_commu_E
	
	local 	  Healthcharac	SP_preagnanttreated_B SP_birthfacility_B SP_FIC_B 	///
							SP_malariatreated_B SP_diarrheatreated_B
														
	foreach	x in SPcharac WKcharac HHcharac Villagecharac Healthcharac {
		
	preserve
	
	if		"`x'" == "SPcharac" | "`x'" == "Healthcharac"   {	
		
	collapse `SPcharac' `Healthcharac' incentives $controls1, by(SUPV_id)
	
	}
		
	foreach	y of varlist ``x'' {
		
	#d	;
		reg	`y' 
			i.incentives
			$controls
		,
		cl(SUPV_id)
		;
	#d	cr 
	
	* Program to set p-values
	set_pval_diff_incentives

	test	2.incentives
	local	pval4 = r(p)
	test 	1.incentives
	local	pval5 = r(p)
	test	3.incentives
	local	pval6 = r(p)
	
	#d	;
		outreg2 
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}
		drop(i.incentives $controls)
		addstat(	Worker Incentives = Supervisor Incentives, $pval1, 
					Worker Incentives = Shared Incentives, $pval3, 
					Supervisor Incentives = Shared, $pval2, 
					Worker Incentives = Control, `pval4', 
					Supv Incentives = Control, `pval5', 
					Shared Incentives = Control, `pval6')
		;
	#d	cr
	
	}
	
	restore

	}
	
}


/*----------------------------------------------------------------------------*/
**## 	2.e Table A.2:	MHT
/*----------------------------------------------------------------------------*/
{
	
	local 	filename = "MHT"
	capture erase "${TABLESDIR}/`filename'.txt"
	capture erase "${TABLESDIR}/`filename'.xls"
	
	preserve
	
	* Part I: Benjamini, Krieger & Yekutieli (2006) values
	** I.1 - Panels A and B
	rename 	HH_nb_topics_discussed_E topic_diss
	rename 	HH_av_visit_length_E av_visit

	local 	outcomes 	HH_nb_visits_E HH_perc_visited_E HH_visit_types_E 		///
						av_visit topic_diss HH_trust_E
	
	local 	k = 1

	gen 	numbering = _n
	gen 	treat_outcome = " "
	gen 	pv =.

	foreach y in `outcomes' {

	#d	;
		reg	`y'
			i.incentives
			$controls
		,
		cl(SUPV_id)
		;
	#d	cr

	test	2.incentives = 0
	local	pv1_`y' = r(p) 	
	replace pv = `pv1_`y'' if numbering == `k'
	
	test 	1.incentives = 0
	local	pv2_`y' = r(p) 	
	replace pv = `pv2_`y'' if numbering == `k' + 1
		
	test	3.incentives = 0
	local	pv3_`y' = r(p) 	
	replace pv = `pv3_`y'' if numbering == `k' + 2
	
	local 	k = `k' + 3

	}

	sum 	pv
	local 	totalpvals = r(N)

	* Sort the p-values in ascending order and generate a variable that codes 
	* each p-value's rank
	gen 	original_sorting_order = _n
	sort 	pv
	gen 	rank = _n if pv !=.

	* Set the initial counter to 1 
	local 	qval = 1

	* Generate the variable that will contain the BKY (2006) sharpened q-values
	gen 	bky06_qval = 1 if pv!=.

	/* 	Set up a loop that begins by checking which hypotheses are rejected at 
		q = 1.000, then checks which hypotheses are rejected at q = 0.999, then 
		checks which hypotheses are rejected at q = 0.998, etc.  The loop ends 
		by checking which hypotheses are rejected at q = 0.001.
	*/


	while 	`qval' > 0 {
		
	* First Stage
	* Generate the adjusted first stage q level we are testing: q' = q/1+q
	local 	qval_adj = `qval'/(1+`qval')
	* Generate value q'*r/M
	gen 	fdr_temp1 = `qval_adj'*rank/`totalpvals'
	* Generate binary variable checking condition p(r) <= q'*r/M
	gen 	reject_temp1 = (fdr_temp1>=pv) if pv!=.
	* Generate variable containing p-value ranks for all p-values that meet 
	*above condition
	gen 	reject_rank1 = reject_temp1*rank
	* Record the rank of the largest p-value that meets above condition
	egen 	total_rejected1 = max(reject_rank1)

	* Second Stage
	* Generate the second stage q level that accounts for hypotheses rejected 
	* in first stage: q_2st = q'*(M/m0)
	local 	qval_2st = `qval_adj'												///
			*(`totalpvals'/(`totalpvals'-total_rejected1[1]))
	* Generate value q_2st*r/M
	gen 	fdr_temp2 = `qval_2st'*rank/`totalpvals'
	* Generate binary variable checking condition p(r) <= q_2st*r/M
	gen 	reject_temp2 = (fdr_temp2>=pv) if pv!=.
	* Generate variable containing p-value ranks for all p-values that meet 
	* above condition
	gen 	reject_rank2 = reject_temp2*rank
	* Record the rank of the largest p-value that meets above condition
	egen 	total_rejected2 = max(reject_rank2)

	* A p-value has been rejected at level q if its rank is less than or equal 
	* to the rank of the max p-value that meets the above condition
	replace bky06_qval = `qval' if rank <= total_rejected2 & rank!=.
	
	* Reduce q by 0.001 and repeat loop
	drop 	fdr_temp* reject_temp* reject_rank* total_rejected*
	local 	qval = `qval' - .001
	
	}

	sort 	original_sorting_order

	global	bky0 = -9.999
	
	forvalues i = 1/`totalpvals' {
		
	global	bky`i' = bky06_qval[`i']
	
	}
	
	drop	numbering treat_outcome pv original_sorting_order rank bky06_qval
	
	** I.2 - Panel C
	rename 	    HH_instit_assisted_birth_E  asstd_birth
	rename 	      HH_PNC_PHUwithin_2days_E 	PNC_2days 
	rename 	  HH_sixmonths_breastfeading_E  six_monbreast
	rename HH_uptodate_child_vaccine_card_E  vacc_card
	rename 	   HH_fever_incidence_below5_E  fever_u5
	rename 	   HH_cough_incidence_below5_E  cough_u5 
	rename HH_diarrhoea_incidence_below5_E  diarrhoea_u5
	rename			   HH_zscore_disease_E  zscore_disease

	
	local	outcomes	HH_zscore_natal_E HH_ANC_atleast4_E asstd_birth 		///
						PNC_2days six_monbreast vacc_card zscore_disease 		///
						fever_u5 cough_u5 diarrhoea_u5
	
	local 	k = 1

	gen 	numbering = _n
	gen 	treat_outcome = " "
	gen 	pv =.

	foreach y in `outcomes' {

	#d	;
		reg	`y'
			i.incentives
			$controls
		,
		cl(SUPV_id)
		;
	#d	cr

	test	2.incentives = 0
	local	pv1_`y' = r(p) 	
	replace pv = `pv1_`y'' if numbering == `k'
	
	test 	1.incentives = 0
	local	pv2_`y' = r(p) 	
	replace pv = `pv2_`y'' if numbering == `k' + 1
		
	test	3.incentives = 0
	local	pv3_`y' = r(p) 	
	replace pv = `pv3_`y'' if numbering == `k' + 2
	
	local 	k = `k' + 3

	}

	sum 	pv
	local 	totalpvals = r(N)

	* Sort the p-values in ascending order and generate a variable that codes 
	* each p-value's rank
	gen 	original_sorting_order = _n
	sort 	pv
	gen 	rank = _n if pv !=.

	* Set the initial counter to 1 
	local 	qval = 1

	* Generate the variable that will contain the BKY (2006) sharpened q-values
	gen 	bky06_qval = 1 if pv!=.

	/* 	Set up a loop that begins by checking which hypotheses are rejected at 
		q = 1.000, then checks which hypotheses are rejected at q = 0.999, then 
		checks which hypotheses are rejected at q = 0.998, etc.  The loop ends 
		by checking which hypotheses are rejected at q = 0.001.
	*/


	while 	`qval' > 0 {
		
	* First Stage
	* Generate the adjusted first stage q level we are testing: q' = q/1+q
	local 	qval_adj = `qval'/(1+`qval')
	* Generate value q'*r/M
	gen 	fdr_temp1 = `qval_adj'*rank/`totalpvals'
	* Generate binary variable checking condition p(r) <= q'*r/M
	gen 	reject_temp1 = (fdr_temp1>=pv) if pv!=.
	* Generate variable containing p-value ranks for all p-values that meet 
	*above condition
	gen 	reject_rank1 = reject_temp1*rank
	* Record the rank of the largest p-value that meets above condition
	egen 	total_rejected1 = max(reject_rank1)

	* Second Stage
	* Generate the second stage q level that accounts for hypotheses rejected 
	* in first stage: q_2st = q'*(M/m0)
	local 	qval_2st = `qval_adj'												///
			*(`totalpvals'/(`totalpvals'-total_rejected1[1]))
	* Generate value q_2st*r/M
	gen 	fdr_temp2 = `qval_2st'*rank/`totalpvals'
	* Generate binary variable checking condition p(r) <= q_2st*r/M
	gen 	reject_temp2 = (fdr_temp2>=pv) if pv!=.
	* Generate variable containing p-value ranks for all p-values that meet 
	* above condition
	gen 	reject_rank2 = reject_temp2*rank
	* Record the rank of the largest p-value that meets above condition
	egen 	total_rejected2 = max(reject_rank2)

	* A p-value has been rejected at level q if its rank is less than or equal 
	* to the rank of the max p-value that meets the above condition
	replace bky06_qval = `qval' if rank <= total_rejected2 & rank!=.
	
	* Reduce q by 0.001 and repeat loop
	drop 	fdr_temp* reject_temp* reject_rank* total_rejected*
	local 	qval = `qval' - .001
	
	}

	sort 	original_sorting_order

	*global	bky0 = -9.999
	local	j = 19
	
	forvalues i = 1/`totalpvals' {
		
	global	bky`j' = bky06_qval[`i']
	local	++j
	
	}
	
	drop	numbering treat_outcome pv original_sorting_order rank bky06_qval

	* Part II: Romano Wolf and Bonferroni
	** II.1 - Panel A: Quantity of visits
	local	outcomes	HH_zscore_qt_E HH_nb_visits_E HH_perc_visited_E
	
	local 	k = 0
	
	#d	;
		rwolf2
		(reg HH_zscore_qt_E
			worker
			supervisor
			shared
			$controls
		,
		cl(SUPV_id)) 
		
		(reg HH_nb_visits_E
			worker
			supervisor
			shared
			$controls
		,
		cl(SUPV_id)) 
		
		(reg HH_perc_visited_E 
			worker 
			supervisor
			shared
			$controls
		,
		cl(SUPV_id))
		
		,
		
		indepvars(	worker supervisor shared, 
					worker supervisor shared,
					worker supervisor shared) 
		reps(${bs_reps_tab}) 
		seed(${seed_tab})
		;
	#d	cr
	
	ereturn list
		
	foreach y in `outcomes' {

	local	p1_`y'_mht = e(rw_`y'_worker) 	
	local	p2_`y'_mht = e(rw_`y'_supervisor) 
	local	p3_`y'_mht = e(rw_`y'_shared) 
	
	}

	foreach y in `outcomes' {
	
	#d	;
		reg	`y' 
			i.incentives
			$controls
		,
		cl(SUPV_id)
		;
	#d	cr

	test	2.incentives = 0
	local	pval1 = r(p) 	
	local	p1_bon = 9 * `pval1'
	if 		`p1_bon' > 1  local p1_bon = 1
	
	test 	1.incentives = 0
	local	pval2 = r(p) 	
	local	p2_bon = 9 * `pval2'
	if 		`p2_bon' > 1  local p2_bon = 1
		
	test	3.incentives = 0
	local	pval3 = r(p) 	
	local	p3_bon = 9 * `pval3' 
	if 		`p3_bon' > 1  local p3_bon = 1
	
	local	pval `pval1' `pval2' `pval3'
	
	if 		"`y'" == "HH_nb_visits_E" local k = 1
	local 	i = `k' + 1
	local 	j = `k' + 2	
	
	if 		"`y'" == "HH_zscore_qt_E" local k = 0
	if 		"`y'" == "HH_zscore_qt_E" local i = 0
	if 		"`y'" == "HH_zscore_qt_E" local j = 0

	#d	;
		outreg2
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}
		drop(i.incentives $controls)
		noobs
		addstat(p-value MHT correction for Worker, `p1_`y'_mht', 
				p-value MHT correction for Supv, `p2_`y'_mht', 
				p-value MHT correction for Shared, `p3_`y'_mht', 
				p-value Bonferroni correction for Worker, `p1_bon', 
				p-value Bonferroni correction for Supv, `p2_bon', 
				p-value Bonferroni correction for Shared, `p3_bon',
				p-value BKY (2006) correction for Worker, ${bky`k'},
				p-value BKY (2006) correction for Supv, ${bky`i'},
				p-value BKY (2006) correction for Shared, ${bky`j'})
		;
	#d	cr
	
	local 	k = `k' + 3
	
	
	}
	 
	** II.2 - Panel B: quality of visits
	local	outcomes 	HH_zscore_ql_E HH_visit_types_E av_visit topic_diss		///
						HH_trust_E
						
	#d	;
		rwolf2 
		(reg HH_zscore_ql_E
			worker
			supervisor
			shared
			$controls
		,
		cl(SUPV_id)) 
		
		(reg HH_visit_types_E 
			worker
			supervisor
			shared
			$controls
		,
		cl(SUPV_id)) 
		
		(reg av_visit 
			worker
			supervisor
			shared
			$controls
		,
		cl(SUPV_id)) 
		
		(reg topic_diss 
			worker
			supervisor
			shared
			$controls
		,
		cl(SUPV_id)) 
		
		(reg HH_trust_E 
			worker
			supervisor
			shared
			$controls
		, cl(SUPV_id))
		
		,
		
		indepvars(	worker supervisor shared, 
					worker supervisor shared,
					worker supervisor shared,
					worker supervisor shared,
					worker supervisor shared)
		reps(${bs_reps_tab}) 
		seed(${seed_tab})
		;
	#d	cr
	
	ereturn list
	
	foreach y in `outcomes' {		

	local	p1_`y'_mht = e(rw_`y'_worker) 	
	local	p2_`y'_mht = e(rw_`y'_supervisor) 
	local	p3_`y'_mht = e(rw_`y'_shared) 
	
	}
	
	foreach y in `outcomes' {		

	#d	;
		reg	`y' 
			i.incentives
			$controls
		,
		cl(SUPV_id)
		;
	#d	cr

	test	2.incentives = 0
	local	pval1 = r(p) 	
	local	p1_bon = 15 * `pval1'
	if 		`p1_bon' > 1  local p1_bon = 1
	
	test 	1.incentives = 0
	local	pval2 = r(p) 	
	local	p2_bon = 15 * `pval2'
	if 		`p2_bon' > 1  local p2_bon = 1
		
	test	3.incentives = 0
	local	pval3 = r(p) 	
	local	p3_bon = 15 * `pval3' 
	if 		`p3_bon' > 1  local p3_bon = 1

	if 		"`y'" == "HH_visit_types_E" local k = 7
	local 	i = `k' + 1
	local 	j = `k' + 2	
	
	if 		"`y'" == "HH_zscore_ql_E" local k = 0
	if 		"`y'" == "HH_zscore_ql_E" local i = 0
	if 		"`y'" == "HH_zscore_ql_E" local j = 0

	#d	;
		outreg2
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}
		drop(i.incentives $controls)
		noobs
		addstat(p-value MHT correction for Worker, `p1_`y'_mht', 
				p-value MHT correction for Supv, `p2_`y'_mht', 
				p-value MHT correction for Shared, `p3_`y'_mht', 
				p-value Bonferroni correction for Worker, `p1_bon', 
				p-value Bonferroni correction for Supv, `p2_bon', 
				p-value Bonferroni correction for Shared, `p3_bon',
				p-value BKY (2006) correction for Worker, ${bky`k'},
				p-value BKY (2006) correction for Supv, ${bky`i'},
				p-value BKY (2006) correction for Shared, ${bky`j'})
		;
	#d	cr
	
	local 	k = `k' + 3
	
	
	}

	** II.3 - Panel C: Health Outcomes 
	local	outcomes	HH_zscore_natal_E HH_ANC_atleast4_E asstd_birth 		///
						PNC_2days six_monbreast vacc_card zscore_disease 		///
						fever_u5 cough_u5 diarrhoea_u5
		
	local	k = 19
						
	#d	;
		rwolf2 
		(reg HH_zscore_natal_E
			worker
			supervisor
			shared
			$controls
		,
		cl(SUPV_id))
		
		(reg HH_ANC_atleast4_E
			worker
			supervisor
			shared
			$controls
		,
		cl(SUPV_id)) 
		
		(reg asstd_birth
			worker
			supervisor
			shared
			$controls
		,
		cl(SUPV_id))
		
		(reg PNC_2days
			worker
			supervisor
			shared
			$controls
		,
		cl(SUPV_id)) 
		
		(reg six_monbreast
			worker
			supervisor
			shared
			$controls
		,
		cl(SUPV_id))
		
		(reg vacc_card
			worker
			supervisor
			shared
			$controls
		,
		cl(SUPV_id)) 
		
		(reg zscore_disease
			worker
			supervisor
			shared
			$controls
		,
		cl(SUPV_id))
		
		(reg fever_u5
			worker
			supervisor
			shared
			$controls
		,
		cl(SUPV_id))
		
		(reg cough_u5
			worker
			supervisor
			shared
			$controls
		,
		cl(SUPV_id))
		
		(reg diarrhoea_u5
			worker
			supervisor
			shared
			$controls
		,
		cl(SUPV_id))
	
		,
		
		indepvars(	worker supervisor shared, 
					worker supervisor shared,
					worker supervisor shared,
					worker supervisor shared,
					worker supervisor shared,
					worker supervisor shared,
					worker supervisor shared,
					worker supervisor shared,
					worker supervisor shared,
					worker supervisor shared) 
		reps(${bs_reps_tab}) 
		seed(${seed_tab})
		;
	#d	cr
	
	ereturn list
	
	foreach y in `outcomes' {
	
	local	p1_`y'_mht = e(rw_`y'_worker) 	
	local	p2_`y'_mht = e(rw_`y'_supervisor) 
	local	p3_`y'_mht = e(rw_`y'_shared) 
	
	}
	
	foreach y in `outcomes' {

	#d	;
		reg	`y' 
			i.incentives
			$controls
		,
		cl(SUPV_id)
		;
	#d	cr

	test	2.incentives = 0
	local	pval1 = r(p) 
	local	p1_bon = 30 * `pval1'
	if 		`p1_bon' > 1  local p1_bon = 1
	
	test 	1.incentives = 0
	local	pval2 = r(p) 
	local	p2_bon = 30 * `pval2'
	if 		`p2_bon' > 1  local p2_bon = 1
	
	test	3.incentives = 0
	local	pval3 = r(p) 
	local	p3_bon = 30 * `pval3' 
	if 		`p3_bon' > 1  local p3_bon = 1

	local 	i = `k' + 1
	local 	j = `k' + 2	

	#d	;
		outreg2
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}
		drop(i.incentives $controls)
		noobs
		addstat(p-value MHT correction for Worker, `p1_`y'_mht', 
				p-value MHT correction for Supv, `p2_`y'_mht', 
				p-value MHT correction for Shared, `p3_`y'_mht', 
				p-value Bonferroni correction for Worker, `p1_bon', 
				p-value Bonferroni correction for Supv, `p2_bon', 
				p-value Bonferroni correction for Shared, `p3_bon',
				p-value BKY (2006) correction for Worker, ${bky`k'},
				p-value BKY (2006) correction for Supv, ${bky`i'},
				p-value BKY (2006) correction for Shared, ${bky`j'})
		;
	#d	cr
	
	local 	k = `k' + 3
	
	}
	
	restore
	
}


/*----------------------------------------------------------------------------*/
**## 	2.f Table A.3:	Visits Type
/*----------------------------------------------------------------------------*/
{
	
	local 	filename = "APPVisitType"
	capture erase "${TABLESDIR}/`filename'.txt"
	capture erase "${TABLESDIR}/`filename'.xls"

	local	outcomes	HH_visit_pregn_E HH_visit_accomp_E 						///
						HH_visit_maternalchild_E HH_zscore_natal_visits_E 		///
						HH_visit_routine_E HH_visit_treat_E HH_visit_follow_E 	///
						HH_zscore_disease_visits_E

	foreach y in `outcomes' {
		
	#d	;
		reg	`y' 
			i.incentives
			$controls
		,
		cl(SUPV_id)
		;
	#d	cr

	sum		`y', meanonly
	local	mean_dep = r(mean)

	sum		`y' if incentives == 0, meanonly
	local	mean_dep_control = r(mean)

	* Program to set p-values
	set_pval_diff_incentives

	#d	;
		outreg2
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}
		drop($controls) 
		sortvar(2.incentives
				1.incentives
				3.incentives)
		addstat(Mean dep. var., `mean_dep', 
				Mean dep. var. in Control, `mean_dep_control', 
				P-value Worker = Supv, $pval1, 
				P-value Supv = Shared, $pval2, 
				P-value Worker = Shared, $pval3) 
		;
	#d	cr 
	
	}

}


/*----------------------------------------------------------------------------*/
**## 	2.g Table A.4:	Targeting
/*----------------------------------------------------------------------------*/
{
	
	local 	filename = "HH_Targets"
	capture erase "${TABLESDIR}/`filename'.txt"
	capture erase "${TABLESDIR}/`filename'.xls"

	local	outcomes	HH_nb_visits_HHAssetsD_E HH_nb_visits_HHDistD_E			///
						HH_nb_visits_HHFamilyD_E HH_nb_visits_HHJointD_E

	foreach y in `outcomes' {
		
	#d	;
		reg	`y' 
			i.incentives
			$controls
		,
		cl(SUPV_id)
		;
	#d	cr

	sum		`y', meanonly
	local	mean_dep = r(mean)

	sum		`y' if incentives == 0, meanonly
	local	mean_dep_control = r(mean)

	* Program to set p-values
	set_pval_diff_incentives

	#d	;
		outreg2
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}
		drop($controls) 
		sortvar(2.incentives
				1.incentives
				3.incentives)
		addstat(Mean dep. var., `mean_dep', 
				Mean dep. var. in Control, `mean_dep_control', 
				P-value Worker = Supv, $pval1, 
				P-value Supv = Shared, $pval2, 
				P-value Worker = Shared, $pval3) 
		;
	#d	cr 
	
	}

}


/*----------------------------------------------------------------------------*/
**## 	2.h Table A.5:	Health More – Services provided by the Health Facility
/*----------------------------------------------------------------------------*/
{
	
	local 	filename = "More_Health_Outcomes"
	capture erase "${TABLESDIR}/`filename'.txt"
	capture erase "${TABLESDIR}/`filename'.xls"

	local	outcomes	HH_fert_E HH_know_E HH_mrate_u5_E

	foreach	y in `outcomes' {

	#d	;
		reg	`y' 
			i.incentives
			$controls
		,
		cl(SUPV_id)
		;
	#d	cr

	sum		`y', meanonly
	local	mean_dep = r(mean)

	sum		`y' if incentives == 0, meanonly
	local	mean_dep_control = r(mean)

	* Program to set p-values
	set_pval_diff_incentives

	#d	;
		outreg2 
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}
		drop($controls) 
		sortvar(2.incentives
				1.incentives
				3.incentives)
		addstat(Mean dep. var., `mean_dep', 
				Mean dep. var. in Control, `mean_dep_control', 
				P-value Worker = Supv, $pval1, 
				P-value Supv = Shared, $pval2, 
				P-value Worker = Shared, $pval3) 
		;
	#d	cr
	
	}

}


/*----------------------------------------------------------------------------*/
**## 	2.i Table A.6:	Health Facility– Services provided by the Health
*						Facility
/*----------------------------------------------------------------------------*/
{
	
	local 	filename = "Health_facility"
	capture erase "${TABLESDIR}/`filename'.txt"
	capture erase "${TABLESDIR}/`filename'.xls"
	
	local 	outcomes	SP_zscore_natal_E SP_preagnanttreated_E 				///
						SP_birthfacility_E SP_FIC_E SP_zscore_disease_E 		///
						SP_malariatreated_E SP_diarrheatreated_E SP_fevercases_E
						
	preserve
						
	collapse  `outcomes' incentives $controls1, by(SUPV_id)

	foreach y in `outcomes' {

	#d	;
		reg	`y'
			i.incentives
			$controls
		,
		cl(SUPV_id)
		;
	#d	cr

	sum		`y', meanonly
	local	mean_dep = r(mean)

	sum		`y' if incentives == 0, meanonly
	local	mean_dep_control = r(mean)

	* Program to set p-values
	set_pval_diff_incentives

	#d	;
		outreg2 
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}
		drop($controls) 
		sortvar(2.incentives
				1.incentives
				3.incentives)
		addstat(Mean dep. var., `mean_dep', 
				Mean dep. var. in Control, `mean_dep_control', 
				P-value Worker = Supv, $pval1, 
				P-value Supv = Shared, $pval2, 
				P-value Worker = Shared, $pval3) 
		;
	#d	cr
	
	}
	
	restore
	
}


/*----------------------------------------------------------------------------*/
**## 	2.j Table A.7:	Supervisor Perceptions and Activities
/*----------------------------------------------------------------------------*/
{

	local 	filename = "PS Effort More"
	capture erase "${TABLESDIR}/`filename'.txt"
	capture erase "${TABLESDIR}/`filename'.xls"
	
	local	outcomes	SP_regular_report_E SP_report_belief_E 					///
						WK_totalSUPVvisits_E WK_calls_E WK_reportwithSUPV_E

	foreach y in `outcomes' {
		
	preserve
		
	if 		"`y'" == "SP_regular_report_E" | "`y'" == "SP_report_belief_E" {	
	collapse `y' incentives $controls1, by(SUPV_id)
	}
	
	#d	;
		reg	`y'
			i.incentives
			$controls
		,
		cl(SUPV_id)
		;
	#d	cr

	sum		`y', meanonly
	local	mean_dep = r(mean)

	sum		`y' if incentives == 0, meanonly
	local	mean_dep_control = r(mean)

	* Program to set p-values
	set_pval_diff_incentives

	#d	;
		outreg2 
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}
		drop($controls) 
		sortvar(2.incentives
				1.incentives
				3.incentives)
		addstat(Mean dep. var., `mean_dep', 
				Mean dep. var. in Control, `mean_dep_control', 
				P-value Worker = Supv, $pval1, 
				P-value Supv = Shared, $pval2, 
				P-value Worker = Shared, $pval3) 
		;
	#d	cr
	
	restore
	
	}
	
}
	

/*----------------------------------------------------------------------------*/
**## 	2.k Table A.8:	Experience–Visits and Supervisor Effort by Worker 
*						Experience
/*----------------------------------------------------------------------------*/
{
	
	local 	filename = "Experience"
	capture erase "${TABLESDIR}/`filename'.txt"
	capture erase "${TABLESDIR}/`filename'.xls"
	
	local	outcomes	HH_nb_visits_E HH_accomp_visits_E SP_shared_inc_E 		///
						WK_reporting_rate_E
	
	foreach y in `outcomes' {

	#d	;
		reg	`y'
			i.WK_exp_abvmedian_B#2.incentives
			i.WK_exp_abvmedian_B#1.incentives
			i.WK_exp_abvmedian_B#3.incentives
			WK_exp_abvmedian_B
			$controls
		,
		cl(SUPV_id) 
		;
	#d	cr

	sum		`y', meanonly
	local 	mean_dep = r(mean)
	sum		`y' if incentives == 0, meanonly
	local	mean_dep_control = r(mean)
	sum		`y' if incentives == 0 & WK_exp_abvmedian_B == 0, meanonly
	local	mean_dep_control_low = r(mean)

	test 	0b.WK_exp_abvmedian_B#2.incentives 									///
			= 0b.WK_exp_abvmedian_B#1bn.incentives
	local	pval1 = r(p)
	test 	0b.WK_exp_abvmedian_B#2.incentives 									///
			= 0b.WK_exp_abvmedian_B#3.incentives
	local	pval2 = r(p)
	test 	0b.WK_exp_abvmedian_B#3.incentives 									///
			= 0b.WK_exp_abvmedian_B#1bn.incentives
	local	pval3 = r(p)

	test 	1.WK_exp_abvmedian_B#2.incentives 									///
			= 1.WK_exp_abvmedian_B#1bn.incentives
	local	pval4 = r(p)
	test 	1.WK_exp_abvmedian_B#2.incentives 									///
			= 1.WK_exp_abvmedian_B#3.incentives
	local	pval5 = r(p)
	test 	1.WK_exp_abvmedian_B#3.incentives 									///
			= 1.WK_exp_abvmedian_B#1bn.incentives
	local	pval6 = r(p)
		
	test 	1.WK_exp_abvmedian_B#2.incentives 									///
			= 0b.WK_exp_abvmedian_B#2.incentives
	local	pval7 = r(p)
	test 	1.WK_exp_abvmedian_B#1bn.incentives 								///
			= 0b.WK_exp_abvmedian_B#1bn.incentives
	local	pval8 = r(p)
	test 	1.WK_exp_abvmedian_B#3.incentives 									///
			= 0b.WK_exp_abvmedian_B#3.incentives
	local	pval9 = r(p) 

	#d	;
		outreg2
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}
		ctitle(`y',`z')
		drop($controls) 
		sortvar(0b.WK_exp_abvmedian_B#2.incentives
				0b.WK_exp_abvmedian_B#1bn.incentives
				0b.WK_exp_abvmedian_B#3.incentives
				1.WK_exp_abvmedian_B#2.incentives
				1.WK_exp_abvmedian_B#1bn.incentives
				1.WK_exp_abvmedian_B#3.incentives
				WK_exp_abvmedian_B)
		addstat(Mean dep. var., `mean_dep', 
				Mean dep. var. in Control low, `mean_dep_control_low', 
				p-value Worker = Supv (Low experience), `pval1', 
				p-value Worker = Shared (Low experience), `pval2', 
				p-value Supv = Shared (Low experience), `pval3', 
				p-value Worker = Supv (High experience), `pval4', 
				p-value Worker = Shared (High experience), `pval5', 
				p-value Supv = Shared (High experience), `pval6', 
				p-value for Worker incentives (Low vs. High), `pval7', 
				p-value for Supv incentives (Low vs. High), `pval8', 
				p-value for Shared incentives (Low vs. High), `pval9')
		;

		reg	`y'
			i.WK_exp_abvmedian_B#2.incentives
			i.WK_exp_abvmedian_B#1.incentives
			i.WK_exp_abvmedian_B#3.incentives
			WK_exp_abvmedian_B
			$controls
			DMEAN*
		,
		cl(SUPV_id)
		;
	#d	cr
	
	break

	sum		`y', meanonly
	local 	mean_dep = r(mean)
	sum		`y' if incentives == 0, meanonly
	local	mean_dep_control = r(mean)
	sum		`y' if incentives == 0 & WK_exp_abvmedian_B == 0, meanonly
	local	mean_dep_control_low = r(mean)

	test 	0b.WK_exp_abvmedian_B#2.incentives 									///
			= 0b.WK_exp_abvmedian_B#1bn.incentives
	local	pval1 = r(p)
	test 	0b.WK_exp_abvmedian_B#2.incentives 									///
			= 0b.WK_exp_abvmedian_B#3.incentives
	local	pval2 = r(p)
	test 	0b.WK_exp_abvmedian_B#3.incentives 									///
			= 0b.WK_exp_abvmedian_B#1bn.incentives
	local	pval3 = r(p)

	test 	1.WK_exp_abvmedian_B#2.incentives 									///
			= 1.WK_exp_abvmedian_B#1bn.incentives
	local	pval4 = r(p)
	test 	1.WK_exp_abvmedian_B#2.incentives 									///
			= 1.WK_exp_abvmedian_B#3.incentives
	local	pval5 = r(p)
	test 	1.WK_exp_abvmedian_B#3.incentives 									///
			= 1.WK_exp_abvmedian_B#1bn.incentives
	local	pval6 = r(p)
		
	test 	1.WK_exp_abvmedian_B#2.incentives 									///
			= 0b.WK_exp_abvmedian_B#2.incentives
	local	pval7 = r(p)
	test 	1.WK_exp_abvmedian_B#1bn.incentives 								///
			= 0b.WK_exp_abvmedian_B#1bn.incentives
	local	pval8 = r(p)
	test 	1.WK_exp_abvmedian_B#3.incentives 									///
			= 0b.WK_exp_abvmedian_B#3.incentives
	local	pval9 = r(p) 

	#d	;
		outreg2
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}
		ctitle(`y',`z')
		drop($controls DMEAN*) 
		sortvar(0b.WK_exp_abvmedian_B#2.incentives
				0b.WK_exp_abvmedian_B#1bn.incentives
				0b.WK_exp_abvmedian_B#3.incentives
				1.WK_exp_abvmedian_B#2.incentives
				1.WK_exp_abvmedian_B#1bn.incentives
				1.WK_exp_abvmedian_B#3.incentives
				WK_exp_abvmedian_B)
		addstat(Mean dep. var., `mean_dep', 
				Mean dep. var. in Control low, `mean_dep_control_low', 
				p-value Worker = Supv (Low experience), `pval1', 
				p-value Worker = Shared (Low experience), `pval2', 
				p-value Supv = Shared (Low experience), `pval3', 
				p-value Worker = Supv (High experience), `pval4', 
				p-value Worker = Shared (High experience), `pval5', 
				p-value Supv = Shared (High experience), `pval6', 
				p-value for Worker incentives (Low vs. High), `pval7', 
				p-value for Supv incentives (Low vs. High), `pval8', 
				p-value for Shared incentives (Low vs. High), `pval9')
		;
	#d	cr
	
	}

}
	

/*----------------------------------------------------------------------------*/
**## 	2.l Table A.9: 	Evidence of Contractual Frictions
/*----------------------------------------------------------------------------*/
{
	
	local 	filename = "Frictions"
	capture erase "${TABLESDIR}/`filename'.txt"
	capture erase "${TABLESDIR}/`filename'.xls"

	#d	;
		reg	SP_shared_inc_E
			i.incentives
			$controls
			if WK_education_B > SP_education_B
		,
		cl(SUPV_id)
		robust
		;
	#d	cr

	sum		SP_shared_inc_E if WK_education_B > SP_education_B, meanonly
	local	mean_dep = r(mean)

	sum		SP_shared_inc_E if incentives == 0 									///
			& WK_education_B > SP_education_B, meanonly
	local	mean_dep_control = r(mean)

	* Program to set p-values
	set_pval_diff_incentives

	#d	;
		outreg2 
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}
		drop($controls) 
		sortvar(2.incentives
				1.incentives
				3.incentives)
		addstat(Mean dep. var., `mean_dep', 
				Mean dep. var. in Control, `mean_dep_control', 
				P-value Worker = Supv, $pval1, 
				P-value Supv = Shared, $pval2, 
				P-value Worker = Shared, $pval3) 
		;

		reg	SP_shared_inc_E 
			i.incentives
			$controls
			if WK_earnless_SUPV_B == 0
		,
		cl(SUPV_id)
		robust
		;
	#d	cr

	sum		SP_shared_inc_E if WK_earnless_SUPV_B == 0, meanonly
	local	mean_dep = r(mean)

	sum		SP_shared_inc_E if incentives == 0 									///
			& WK_earnless_SUPV_B == 0, meanonly
	local	mean_dep_control = r(mean)

	* Program to set p-values
	set_pval_diff_incentives

	#d	;
		outreg2 
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}
		drop($controls) 
		sortvar(2.incentives
				1.incentives
				3.incentives)
		addstat(Mean dep. var., `mean_dep', 
				Mean dep. var. in Control, `mean_dep_control', 
				P-value Worker = Supv, $pval1, 
				P-value Supv = Shared, $pval2, 
				P-value Worker = Shared, $pval3) 
		;

		reg	WK_SUPVearn_reports_net_E
			i.incentives
			$controls
		,
		cl(SUPV_id)
		robust
		;
	#d	cr

	sum		WK_SUPVearn_reports_net_E, meanonly
	local	mean_dep = r(mean)

	sum		WK_SUPVearn_reports_net_E if incentives == 0, meanonly
	local	mean_dep_control = r(mean)

	* Program to set p-values
	set_pval_diff_incentives

	#d	;
		outreg2 
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}
		drop($controls) 
		sortvar(2.incentives
				1.incentives
				3.incentives)
		addstat(Mean dep. var., `mean_dep', 
				Mean dep. var. in Control, `mean_dep_control', 
				P-value Worker = Supv, $pval1, 
				P-value Supv = Shared, $pval2, 
				P-value Worker = Shared, $pval3) 
		;
	#d	cr

}
	

/*----------------------------------------------------------------------------*/
**## 	2.m Table A.10:	Job Satisfaction
/*----------------------------------------------------------------------------*/
{
	
	local 	filename = "Satisfaction"
	capture erase "${TABLESDIR}/`filename'.txt"
	capture erase "${TABLESDIR}/`filename'.xls"
	
	local 	 WorkerSat	WK_satisfied_incentives_E WK_satisfied_org_E 			///
						WK_satisfied_job_E
	
	local 	   SupvSat	SP_satisfied_incentives_E SP_satisfied_org_E 			///
						SP_satisfied_job_E
	
	local 	WorkerPerc	WK_competitive_E WK_identity_E
	
	foreach	x in WorkerSat SupvSat WorkerPerc {
		
	preserve
	
	if		"`x'" == "SupvSat" {	
		
	collapse incentives $controls1 `SupvSat', by(SUPV_id)
	
	}

	foreach	y of varlist ``x'' {

	#d	;
		reg	`y' 
			i.incentives
			$controls
		,
		cl(SUPV_id)
		;
	#d	cr
	
	if		"`y'" == "SupvSat" {												
	
	#d	;
		reg	`y'
			i.incentives
			$controls
		,
		robust
		;
	#d	cr
	
	}

	sum		`y', meanonly
	local	mean_dep = r(mean)

	sum		`y' if incentives == 0, meanonly
	local	mean_dep_control = r(mean)

	* Program to set p-values
	set_pval_diff_incentives

	#d	;
		outreg2 
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}
		drop($controls) 
		sortvar(2.incentives
				1.incentives
				3.incentives)
		addstat(Mean dep. var., `mean_dep', 
				Mean dep. var. in Control, `mean_dep_control', 
				P-value Worker = Supv, $pval1, 
				P-value Supv = Shared, $pval2, 
				P-value Worker = Shared, $pval3) 
		;
	#d	cr
	
	}
	
	restore
	
	}
	
}


/*----------------------------------------------------------------------------*/
**## 	2.n Table A.15:	Visits No Merit – Incentives and Household Visits, 		
* 						Restricted Sample of Health Workers
/*----------------------------------------------------------------------------*/
{
	
	local 	filename = "APPVisitNoMerit"
	capture erase "${TABLESDIR}/`filename'.txt"
	capture erase "${TABLESDIR}/`filename'.xls"

	local	outcomes	HH_nb_visits_E HH_av_visit_length_E 					///
						HH_nb_topics_discussed_E HH_perc_visited_E 				///
						HH_visit_types_E HH_trust_E

	foreach y in `outcomes' {

	#d	;
		reg	`y'
			i.incentives
			$controls
			if merit_survey == 0
		,
		cl(SUPV_id)
		;
	#d	cr

	sum		`y' if merit_survey == 0, meanonly
	local	mean_dep = r(mean)

	sum		`y' if incentives == 0 & merit_survey == 0, meanonly
	local	mean_dep_control = r(mean)

	* Program to set p-values
	set_pval_diff_incentives
	
	#d	;
		outreg2 
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}
		drop($controls)
		sortvar(2.incentives
				1.incentives
				3.incentives)
		addstat(Mean dep. var., `mean_dep', 
				Mean dep. var. in Control, `mean_dep_control', 
				P-value Worker = Supv, $pval1, 
				P-value Supv = Shared, $pval2, 
				P-value Worker = Shared, $pval3) 
		;
	#d	cr
	
	}
	
}


/*----------------------------------------------------------------------------*/
**## 	2.o Table A.16:	Treatment Assignments
/*----------------------------------------------------------------------------*/
{
	
	local 	filename = "Orth Merit1"
	capture erase "${TABLESDIR}/`filename'.txt"
	capture erase "${TABLESDIR}/`filename'.xls"
	
	preserve
	
	collapse incentives Tmerit Tpay, by(SUPV_id)


	#d	;
		reg	Tmerit
			i.incentives
		,
		robust
		;

		outreg2
		using "${TABLESDIR}/`filename'.xls"
		,
		nor2 
		bdec(3) 
		nocons 
		excel 
		label
		sortvar(2.incentives
				1.incentives
				3.incentives)
		;

		
		reg	Tpay
			i.incentives
		,
		robust
		;

		outreg2
		using "${TABLESDIR}/`filename'.xls"
		,
		nor2 
		bdec(3) 
		nocons 
		excel 
		label
		sortvar(2.incentives
				1.incentives
				3.incentives)
		;
	#d	cr
	
	restore

}


/*----------------------------------------------------------------------------*/
**## 	2.p Table A.17:	Balance Checks – All Pairwise Treatment Comparisons
/*----------------------------------------------------------------------------*/
{
	
	local 	filename = "Orth Merit2"
	capture erase "${TABLESDIR}/`filename'.txt"
	capture erase "${TABLESDIR}/`filename'.xls"
	
	local	      SPcharac	SP_male_B SP_age_B SP_primary_educ_B 				///
							SP_secondary_above_educ_B SP_wealth_B SP_nb_WKs_B

	local 	      WKcharac	WK_male_B WK_age_B WK_primary_educ_B				///
							WK_secondary_above_educ_B WK_wealth_B 				///
							WK_nb_HHs_B WK_dist_SUPV_B
						
	local 	      HHcharac	HH_age_B HH_primary_educ_E 							///
							HH_secondary_above_educ_E HH_wealth_B 				///
							HH_dist_village_E
						
	local 	 Villagecharac	WK_road_phu_commu_E WK_mobile_net_commu_E

	local 	  Healthcharac	SP_preagnanttreated_B SP_birthfacility_B SP_FIC_B	///
							SP_malariatreated_B SP_diarrheatreated_B
	
	foreach	x in SPcharac WKcharac HHcharac Villagecharac Healthcharac {
		
	preserve
	
	if		"`x'" == "SPcharac" | "`x'" == "Healthcharac"   {	
		
	collapse `SPcharac' `Healthcharac' incentives merit_treatment				///
			, by(SUPV_id)
	
	}

	foreach	y of varlist ``x'' {
		
	#d	;
		reg	`y' 
			i.incentives##i.merit_treatment
		,
		cl(SUPV_id)
		;
	#d	cr
	
	test	2.incentives#1.merit_treatment = 2.incentives
	local	c1`y' = r(p) 
	test	3.incentives#1.merit_treatment = 3.incentives
	local	c2`y' = r(p) 
	test 	1.incentives#1.merit_treatment = 1.incentives
	local	c3`y' = r(p)
	test	2.incentives#2.merit_treatment = 2.incentives
	local	c4`y' = r(p)
	test 	1.incentives#2.merit_treatment = 1.incentives
	local	c5`y' = r(p)
	test	3.incentives#2.merit_treatment = 1.incentives
	local	c6`y' = r(p)
	
	#d	;
		outreg2 
		using "${TABLESDIR}/`filename'.xls"
		,
		${outreg0ptions}
		drop(i.incentives##i.merit_treatment)
		noobs
		addstat(	Meritocraticy & Worker Incentives 
					= No Meritocracy & Worker Incentives , `c1`y'', 
					Meritocraticy & Supv Incentives 
					= No Meritocracy & Supv Incentives, `c2`y'', 
					Meritocraticy & Shared Incentives 
					= No Meritocracy & Shared Incentives, `c3`y'', 
					Info & Worker Incentives 
					= No Info & Worker Incentives, `c4`y'', 
					Info & Supv Incentives 
					= No Info & Supv Incentives, `c5`y'', 
					Info & Shared Incentives 
					= No Info & Shared Incentives , `c6`y'')
		;
	#d	cr
	
	}
	
	restore
	
	}
	
}



******************************** End of do-file ********************************	

